/*
 * AutoAnswer
 * Copyright (C) 2010 EverySoft
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This file incorporates work covered by the following copyright and
 * permission notice:
 *
 *   Copyright (C) 2010 Tedd Scofield
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.callbox.frontend;

import java.io.IOException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.android.internal.telephony.ITelephony;
import com.callbox.R;
import com.callbox.dao.ClsMessageDAO;
import com.callbox.entity.ClsMessages;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import android.view.KeyEvent;

public class AutoAnswerIntentService extends IntentService {
	
	public String CallNumberIncoming = "";
	
	
	static Context contextGeneral = null;
	static String FinalPathMessage = "";
	
	/*Context currentContext = this;*/

	public AutoAnswerIntentService() {
		super("AutoAnswerIntentService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Context context = getBaseContext();
		contextGeneral = context;

		// Load preferences
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		BluetoothHeadset bh = null;
		if (prefs.getBoolean("headset_only", false)) {
			bh = new BluetoothHeadset(this, null);
		}

		// Let the phone ring for a set delay
		try {
			Thread.sleep(Integer.parseInt(prefs.getString("delay", "2")) * 1000);
		} catch (InterruptedException e) {
			// We don't really care
		}

		// Check headset status right before picking up the call
		if (prefs.getBoolean("headset_only", false) && bh != null) {
			if (bh.getState() != BluetoothHeadset.STATE_CONNECTED) {
				bh.close();
				return;
			}
			bh.close();
		}

		// Make sure the phone is still ringing
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getCallState() != TelephonyManager.CALL_STATE_RINGING) {
			return;
		}

		// Answer the phone
		try {
			contextGeneral = context;
			answerPhoneAidl(context);
			startVoiceMail(context);
		}
		catch (Exception e) {
			e.printStackTrace();
			//Log.d("AutoAnswer","Error trying to answer using telephony service.  Falling back to headset.");
			answerPhoneHeadsethook(context);
		}

		// Enable the speakerphone
		if (prefs.getBoolean("use_speakerphone", false)) {
			enableSpeakerPhone(context);
		}
		
		return;
	}
	private Timer myTimer;
	static RecordTest record;
	static RecordCall Grabacion = new RecordCall();
	
	

	private void startVoiceMail(Context cont)
	{
		
		myTimer =new Timer();
 
		TimerTask timerTask = new TimerTask() 
	        { 
	            public void run()  
	            { 
	            	Call=true;
	            	FinalPathMessage = Grabacion.onRecord();//METODO PARA COMENZAR A GRABAR
	        	      myTimer.cancel();
	            } 
	        }; 
 
			myTimer.schedule(timerTask, 4000);
		
		record = new RecordTest();
		
		 MediaPlayer mp = MediaPlayer.create(AutoAnswerIntentService.this, R.raw.contestadora);        
	        mp.start();
	//	 Log.v("dhidalgo","Start voicemail");
		 	
	        mp.setOnCompletionListener(new OnCompletionListener() {	        	
	        	public void onCompletion(MediaPlayer mp) {
	                // TODO Auto-generated method stub
	            	//Log.v("dhidalgo","Start Record");
	            	mp.stop();
	                mp.release();
	                //record.onRecord(true);	                
	            }
	        });	        
	        mp.start();	        
	}

	private void enableSpeakerPhone(Context context) {
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		audioManager.setSpeakerphoneOn(true);
	}

	private void answerPhoneHeadsethook(Context context) {
		// Simulate a press of the headset button to pick up the call
		Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);		
		buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
		context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");

		// froyo and beyond trigger on buttonUp instead of buttonDown
		Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);		
		buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
		context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
	}
	//ListenToPhoneState listener;
	@SuppressWarnings("unchecked")
	private void answerPhoneAidl(Context context) throws Exception {
		// Set up communication with the telephony service (thanks to Tedd's Droid Tools!)
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		/*
		listener = new ListenToPhoneState();
		tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
		*/
		Class c = Class.forName(tm.getClass().getName());
		Method m = c.getDeclaredMethod("getITelephony");
		m.setAccessible(true);
		ITelephony telephonyService;
		telephonyService = (ITelephony)m.invoke(tm);

		// Silence the ringer and answer the call!
		telephonyService.silenceRinger();
		telephonyService.answerRingingCall();
	}
	static boolean Call=false;
	
	public class ListenToPhoneState extends PhoneStateListener {
		
		
        public void onCallStateChanged(int state, String incomingNumber) {
        	
        	CallNumberIncoming = incomingNumber;
        	
            //Log.v("dhidalgo","Call state -> " +  stateName(state));
        	if(stateName(state).equals("Idle"))
            {
        	//	Log.v("dhidalgo","Stop record");
        		if(Call){
	        		Grabacion.stopRecord();//METODO PARA DETENER LA GRABACION
	        		Call=false;
	        		
	        		ClsMessages m = new ClsMessages();
	        		
	        		
	        		Calendar c = Calendar.getInstance();   
	        		  
	        		SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");  
	        		String formattedDate1 = df1.format(c.getTime());   
	        		          
	        		SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy HH:mm a");  
	        		String formattedDate3 = df3.format(c.getTime());
	        		
	        		m.setNumberFrom("099062274");
	        		m.setReceiptDate(formattedDate1);
	        		m.setDuration("00:17");
	        		m.setTime(formattedDate3.split(" ")[1]);
	        		m.setName("Pepe Roni");
	        		m.setPathResource(FinalPathMessage);        		
	        		
	        		ClsMessageDAO.Instance().Insert(AutoAnswerIntentService.contextGeneral, m);
	        		
	        	//	MessageNotification n = new MessageNotification(AutoAnswerIntentService.this);
	        		//n.createMessageNotification();
        		}
            	////record.onRecord(false);
            	//Log.v("dhidalgo","Play recording");
            	//record.onPlay(true);
            }
        }

        String stateName(int state) {
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE: return "Idle";
                case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook";
                case TelephonyManager.CALL_STATE_RINGING: return "Ringing";
            }
            return Integer.toString(state);
        }
    }
}
